package com.qfedu.fy.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.fy.dao.DepartmentDao;
import com.qfedu.fy.dto.DepartmentNumsDto;
import com.qfedu.fy.entity.Department;
import com.qfedu.fy.entity.User;
import com.qfedu.fy.service.intf.DepartmentService;
import com.qfedu.fy.vo.PageBeanVo;
import com.qfedu.fy.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentDao dao;

    @Override
    public R add(Department department) {
        if(dao.add(department)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public PageBeanVo selectAll(int page,int limit) {
        //1.开启分页
        PageHelper.startPage(page, limit);
        //2.获取查询的结果
        PageInfo<Department> pageInfo=new PageInfo<>(dao.selectAll());
        //3.返回结果
        return new PageBeanVo(pageInfo.getTotal(),pageInfo.getList());
    }



    @Override
    public R update(Department department) {
        if(dao.update(department) > 0){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    @Override
    public R del(int id) {
        if(dao.del(id) > 0){
            return R.ok();
        }else{
            return R.fail();
        }
    }
    @Override
    public R all() {
        return R.ok(dao.selectAll());
    }

    @Override
    public void createExcel(HttpServletResponse response) throws IOException {
        //1.查询数据
        List<Department> list=dao.selectAll();
        //2.设置文件下载
        //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
        response.setHeader("content-disposition","attachment;filename=department_"+System.currentTimeMillis()+".xlsx");
        //3.生成Excel并写出
        EasyExcel.write(response.getOutputStream(),Department.class).sheet(System.currentTimeMillis()+"").doWrite(list);
    }

    @Override
    public R uploadExcel(MultipartFile file) throws IOException {
        //1.获取上传的内容 2.解析文件 3.批量新增
        //1.验证上传文件是否非空
        if(!file.isEmpty()){
            //2.获取上传内容
            InputStream is=file.getInputStream();

            //3.解析Excel
            EasyExcel.read(is,Department.class,new PageReadListener<Department>((list)->{
                //4.调用 dao 实现批量新增
                dao.addBatch(list);
            })).sheet().doRead();
        }
        return R.ok();
    }

    @Override
    public R selectNums() {
        //1.查询数据-数据库
        List<DepartmentNumsDto> list=dao.selectNums();

        //2.整理数据 符合前端要求 2个集合
        List<Integer> nums=new ArrayList<>(list.size());
        List<String> names=new ArrayList<>(list.size());

        //3.遍历集合 组装新数据 函数式编程
        list.forEach(d->{
            nums.add(d.getNums());
            names.add(d.getName());
        });

        //4.创建Map集合 存储要返回数据
        HashMap<String,Object> map=new HashMap<>();
        map.put("names",names);
        map.put("nums",nums);

        return R.ok(map);
    }

}
